package com.intellij.ide.license.impl;

import com.intellij.ide.license.LicenseFactory;
import com.intellij.ide.license.LicenseMessages;
import com.intellij.licensecommon.data.LicenseData;

import javax.swing.*;
import java.io.*;
import java.util.Date;

/*
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.application.ex.ApplicationInfoEx;
import com.intellij.openapi.application.impl.ApplicationInfoImpl;
import com.intellij.openapi.util.io.FileUtil;
*/

public abstract class LicenseFactoryImpl implements LicenseFactory {
    private final LicenseMessages licenseMessages;

    public LicenseFactoryImpl(LicenseMessages licenseMessages) {
        this.licenseMessages = licenseMessages;
    }

    public LicenseMessages getLicenseMessages() {
        return this.licenseMessages;
    }

    public ServerLicense a(String paramString1, long paramLong, String paramString2, String paramString3, int paramInt, Date paramDate) {
        return new ServerLicense(paramString1, paramLong, paramString2, paramString3, paramInt, getLicenseType(), paramDate);
    }

    public LicenseData read() {
        File file = getEvaluationLicenseFile();
        if (file.exists()) {
            try {
                DataInputStream dis = new DataInputStream(new FileInputStream(file));
                long generationTime = dis.readLong() ^ 0xFFFFFFFF;
                dis.close();
                return doCreateEvaluationLicense(generationTime);
            } catch (IOException ioe) {
                return null;
            }
        }
        return null;
    }

    protected abstract EvaluationLicense doCreateEvaluationLicense(long generationTime);


    @Override
    public EvaluationLicense createEvaluationLicense() {
        long now = new Date().getTime();
        EvaluationLicense license = doCreateEvaluationLicense(now);
        if (license == null) {
            throw new IllegalStateException(String.format(" method %s.%s must not return null", new Object[]{"com/intellij/ide/license/impl/LicenseFactoryImpl", "createEvaluationLicense"}));
        }
        return license;
    }

    @Override
    public boolean saveEvaluationLicense(EvaluationLicense license) {
        try {
            File licenseFile = getEvaluationLicenseFile();
            // TODO
            // FileUtil.createIfDoesntExist(licenseFile);

            DataOutputStream dos = new DataOutputStream(new FileOutputStream(licenseFile));
            dos.writeLong(license.getGenerationDate().getTime() ^ 0xFFFFFFFF);
            dos.close();
            return true;
        } catch (IOException localIOException) {
            JOptionPane.showMessageDialog(null, "Error saving evaluation license: " + localIOException.getMessage());
        }
        return false;
    }

    protected File getEvaluationLicenseFile() {
        /*
        ApplicationInfoEx localApplicationInfoEx = m();
        String str = (localApplicationInfoEx.isEAP()) && (!k()) ? localApplicationInfoEx.getBuild().asString() : getValidatorVersion();

        return new File(PathManager.getConfigPath() + File.separator + "eval" + File.separator + getProductName() + str + ".evaluation.key");
        */
        return new File("");
    }

    /*
    protected static ApplicationInfoEx m() {
        return ApplicationInfoImpl.getShadowInstance();
    }
    */
}
